package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.dto.BrandDto;
import com.wnxy.pawnbackgroundsystem.dto.BrandPageDto;
import com.wnxy.pawnbackgroundsystem.dto.userDto.GetGoodsBrandDto;
import com.wnxy.pawnbackgroundsystem.entity.Brand;
import com.wnxy.pawnbackgroundsystem.dao.BrandMapper;
import com.wnxy.pawnbackgroundsystem.entity.BrandSort;
import com.wnxy.pawnbackgroundsystem.param.BrandJuniorParam;
import com.wnxy.pawnbackgroundsystem.param.GetBrandParam;
import com.wnxy.pawnbackgroundsystem.response.Result;
import com.wnxy.pawnbackgroundsystem.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.pawnbackgroundsystem.service.IBrandSortService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品品牌表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
    @Autowired
    private IBrandSortService iBrandSortService;

    //分页带条件查询品牌
    public BrandPageDto getBrand(GetBrandParam getBrandParam){
        QueryWrapper<Brand> qw = new QueryWrapper<>();
        qw.orderByDesc("brand_id");
        if(null!=getBrandParam.getBrandNameInitial()){
            qw.like("brand_name",getBrandParam.getBrandNameInitial())
                    .or()
                    .like("brand_initial",getBrandParam.getBrandNameInitial());
        }

        Page<Brand> page = this.page(new Page<>(getBrandParam.getPageParam().getIndexPage(), getBrandParam.getPageParam().getSize()), qw);
        ArrayList<BrandDto> brandDtos = new ArrayList<>();
        for(Brand br:page.getRecords()){
            BrandDto brandDto = new BrandDto();
            BeanUtils.copyProperties(br,brandDto);
            brandDtos.add(brandDto);
        }
        BrandPageDto brandPageDto = new BrandPageDto();
        brandPageDto.setLb(brandDtos);
        brandPageDto.setIndexPage(page.getCurrent());
        brandPageDto.setMaxPage(page.getPages());
        brandPageDto.setTotle(page.getTotal());

        return brandPageDto;
    }
    //删除品牌
    public void delBrand(List<Integer> intList){
        this.removeByIds(intList);
        for(Integer ii:intList){
            iBrandSortService.remove(new UpdateWrapper<BrandSort>().eq("brand_id",ii));
        }
    }
    //新增修改品牌
    public void brandJunior(BrandJuniorParam brandJuniorParam){
        System.out.println(brandJuniorParam);
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandJuniorParam,brand);
        this.saveOrUpdate(brand);
        List<Integer> sortIds = brandJuniorParam.getSortIds();
        //修改
        if(null!=brandJuniorParam.getBrandId()){
            iBrandSortService.remove(new UpdateWrapper<BrandSort>().eq("brand_id",brandJuniorParam.getBrandId()));
            iBrandSortService.remove(new UpdateWrapper<BrandSort>().eq("brand_id",brandJuniorParam.getBrandId()));
            for (Integer ii:brandJuniorParam.getSortIds()){
                BrandSort brandSort = new BrandSort();
                brandSort.setBrandId(brandJuniorParam.getBrandId());
                brandSort.setSortId(ii);
                iBrandSortService.save(brandSort);
            }
        //新增
        }else {
            for (Integer ii:brandJuniorParam.getSortIds()){
                BrandSort brandSort = new BrandSort();
                brandSort.setBrandId(brand.getBrandId());
                brandSort.setSortId(ii);
                iBrandSortService.save(brandSort);
            }
        }
    }
    //商品查询全部品牌
    public List<GetGoodsBrandDto> getGoodsBrand(){
        List<Brand> brands = this.list();
        ArrayList<GetGoodsBrandDto> getGoodsBrandDtos = new ArrayList<>();
        for(Brand br:brands){
            GetGoodsBrandDto getGoodsBrandDto = new GetGoodsBrandDto();
            BeanUtils.copyProperties(br,getGoodsBrandDto);
            getGoodsBrandDtos.add(getGoodsBrandDto);
        }
        return getGoodsBrandDtos;
    }
}
